package com.gaojiawei.multiThread02_ThreadStatus;

import org.junit.Test;
import sun.java2d.loops.TransformHelper;

/**
 * 线程大的七种状态
 */
public class SevenThreadStatus {
    /**
     * 线程的新建状态
     */
    @Test
    public void testNew(){
        Thread thread = new Thread(() -> {
        });
        System.out.println(thread.getState());
    }

    /**
     * 线程的运行状态
     */
    @Test
    public void testRunable() throws InterruptedException {
        Thread thread = new Thread(() -> {
            while (true) {

            }
        });
        thread.start();
        Thread.sleep(1000);
        System.out.println(thread.getState());
    }

    /**
     * 线程的阻塞状态
     */
    @Test
    public void testBlock() throws InterruptedException {
        //创建锁资源
        Object o = new Object();
        Thread thread = new Thread(() -> {
            //线程尝试获取锁资源
            synchronized (o) {

            }
        });
        //主线程已经拿到锁资源
        synchronized (o){
            thread.start();
            Thread.sleep(1000);
            System.out.println(thread.getState());
//            Thread.sleep(1000);


        }
    }

    /**
     * 线程wait状态
     */
    @Test
    public void testWati() throws InterruptedException {
        Object o = new Object();
        Thread thread = new Thread(() -> {
            synchronized (o){
                try {
                    o.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        });
        thread.start();
        Thread.sleep(1000);
        System.out.println(thread.getState());
    }

    /**
     * 线程的timewait状态
     * @throws InterruptedException
     */
    @Test
    public void testTimeWait() throws InterruptedException {
        Object o = new Object();
        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        thread.start();
        Thread.sleep(500);
        System.out.println(thread.getState());
    }

    /**
     * 线程的terminnate状态
     * @throws InterruptedException
     */
    @Test
    public void testTerminate() throws InterruptedException {
        Object o = new Object();
        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        thread.start();
        Thread.sleep(2000);
        System.out.println(thread.getState());
    }



}
